//
//  MKNetworkingTool.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/5/12.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class MKNetworkingTool: NSObject {
    
    static let sharedInstance: MKNetworkingTool = MKNetworkingTool()
    
    private let afnManager: AFHTTPSessionManager = {
         let afn = AFHTTPSessionManager(baseURL: NSURL(string:"https://api.weibo.com/") as URL?)
//        afn.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain","application/json","Type-Text") as? Set<String>

        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return afn
    }()
    
    func request(method: RequestMethod,URLString: String,parameters: AnyObject,success:@escaping ((URLSessionDataTask,Any?)->Void),failure:@escaping ((URLSessionDataTask?,Error) -> Void)) {
        
        if method == RequestMethod.GET {
            afnManager.get(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else {
            afnManager.post(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }

    }
}
